package stdio.costa.moca.rest;

import java.io.ByteArrayInputStream;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

import stdio.costa.moca.model.Postcard;

@Path("/pdf")
public class PdfResource extends RestfulOperations {
	
	@Path("/{postcardUuid}")
	@GET
	@Produces("application/pdf")
	public Response getPdf(@PathParam("postcardUuid") String postcardUuid) {
		logger.info("Getting pdf...");
		Postcard postcard = getEntityManager().find(Postcard.class, postcardUuid);
		if(postcard.isPdfGenerated()) {
			return Response.ok(new ByteArrayInputStream(postcard.getPdf())).build();
		} else {
			return Response.status(Response.Status.NOT_FOUND).build();
		}
	}
}
